package xyz.mvfast.util;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author fuzeqiang
 * @projectName easy-import
 * @title: DateTimeUtil
 * @date 2021/5/24 16:36
 */
@Slf4j
public class DateTimeUtil {


    /**
     * 时间格式化
     */
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 文件时间前缀
     */
    public static final String YYYY_MM_DD_HH_MM_SS_FILE = "yyyy_MM_dd_HH_mm_ss";


    /**
     * 日期格式化
     * @param date 日期数据
     * @param format 需要格式化的样式
     * @return
     */
    public static String dateTimeToString(Date date, String format){
        try {
            if (date == null){
                return "";
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        }catch (Exception e){
            log.error("parse error", e);
        }
        return "";
    }


    /**
     * 毫秒转中文显示
     * @param timemillis
     * @return
     */
    public static String timemillisToString(Long timemillis){
        try {
            if (timemillis < 1000L){
                return "<1秒";
            }
            Integer ss = 1000;
            Integer mi = ss * 60;
            Integer hh = mi * 60;
            Integer dd = hh * 24;
            Long day = timemillis / dd;
            Long hour = (timemillis - day * dd) / hh;
            Long minute = (timemillis - day * dd - hour * hh) / mi;
            Long second = (timemillis - day * dd - hour * hh - minute * mi) / ss;
            StringBuffer sb = new StringBuffer();
            if(day > 0) {
                sb.append(day+"天");
            }
            if(hour > 0) {
                sb.append(hour+"小时");
            }
            if(minute > 0) {
                sb.append(minute+"分");
            }
            if(second > 0) {
                sb.append(second+"秒");
            }
            return sb.toString();
        }catch (Exception e){
            log.error("parse error", e);
        }
        return "";
    }

    /**
     * 字符串转时间
     * @param value
     * @param dateFormat
     * @return
     */
    public static Object stringToDate(String value, String dateFormat) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            date = sdf.parse(value);
        }catch (Exception e){
            log.error("parse date time failed ...", e);
        }
        return date;
    }

}
